<?php
/*-----------------------------------------
MAGIC - User System Framework

This class is used to both define and handle all interactions with a user.

@Version: 2009.05.23. 21:16
@Author: Carl
@Licence: LGPL -  http://www.gnu.org/licenses/lgpl.html
@Copyright (c) thybag.co.uk 2009
-----------------------------------------*/
Class User extends Database {

	
	private $uid;
	private $password;//password
	private $permissions;//permissions object
	private $details = Array();
	
	private $isShell = false;
	private $isSelf;
	
	static function passwordHash($password,$salt){
		return sha1(strtoupper(md5($password)).$salt);
	}
	private function isSelf(){
		return true; // is this object the logged in user
	}#
	
	private function isShell(){
		return $this->isShell; //is shell / guest? - if so we dont know the tables? omgz
	}
	/*
	Construct
	Provided with a array, most commonly directly from the database,  will  store all of users details
	*/
	public function __construct($uid=0){
	
		parent::__construct();
		
		$this->uid = $uid;
		
		
		
		if($uid !== 0){
			//Attempt to load self
			$this->details = $this->get_row('users','uid',$this->uid);
			if($this->details !== null){ //if db query got somthing. setting uid has little effect on program
				//proccess special values (if feild doesnt exist or permission_id is wrong will return basic Guest perms) 
				$this->permissions = new Permissions($this->details['permissions']);
				$this->password = $this->details['pass'];
				$this->details['pass'] = ''; // blank feild to avoid unwanted accesses
				$this->isShell = false;
				return;
			}
		}
		//if UID does equal zero or user could not be found setup a shell user
		
		//Load the one user we can rely on to get db structure;
		$shell = $this->get_row('users','uid',1);
		if($shell == null) die('Houston... we have a problem (your admin is missing in action)');// throw db error exception
		
		//Blank all are data, we dont wana go round just handing out admin level controls after all
		foreach($shell as $key=>$value){
			$shell[$key] = '';
		}
		$shell['uid'] = '0';
		$shell['name'] = Reg::get('guest_name');
	
		$this->details = $shell;
		$this->isShell = true;
		$this->permissions = new Permissions(Reg::get('guest_permission'));
	}
	
	public function write(){

		//Load are details to an array (since this contains all are info)
		$userDataArray = $this->details;
		//override specal details
		$userDataArray['uid'] = $this->uid;
		$userDataArray['pass'] = $this->password;
		
		if($this->isShell){
			//Maintain username uniqueness
			if($userDataArray['name'] == '')return false;
			if($this->get_row('users','name',$userDataArray['name']) !== null) return false;
		
			//id will be ignored so no worrys with setting it
			$this->create_row('users',$userDataArray);
		}else{
			//update current details
			//Write ourselves and all are stats back to the database!

			$this->set_row('users','uid',$this->uid,$userDataArray); // now we pray
		}
		return true;
	}
	
	/*
	getPassword
	returns users password
	*/
	public function getPassword(){
		 return $this->password;
	}
	/*
	setPassword
	returns users password
	*/
	public function setPassword($newpassword){
		 $this->password = User::passwordHash($newpassword,Reg::get('salt'));
	}
	
	

	
	/*
	getPermissions
	returns users permissions object
	*/
	public function getPermissions(){
		 return $this->permissions;
	}
	public function setPermissions($perm){
		 $this->permissions = $perm;
	}
	/*
	getUid
	returns uid
	*/
	public function getUid(){
		 return $this->uid;
	}
	/*
	getAttribute
	get single Attribute from users data array
	*/
	public function get($attribute){
		if(isset($this->details[$attribute])){
			return $this->details[$attribute]; // if the attribute exists, grab it
		}else{
			return 'no value'; // else return error
		}
	}
	/*
	setAttribute
	Attempts to update the value on an item stored in users data array
	*/
	public function set($attribute,$value){
		if(isset($this->details[$attribute])){//if attribute exists
			$this->details[$attribute] = $value;//else  set value and return true
			return true;
		}else{
			return false;
		}
	}

	/*
	getAttriubuteArray
	return all users attributes as an array
	*/
	public function getAsArray(){
		return $this->details;
	}
	

	
	
	
}